//
//  ProductLinkVC.m
//  danpin
//
//  Created by chuangjia on 20/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ProductLinkVC.h"
#import "viewconfig.h"
#import "UIPlaceholderTextView.h"
#import "ProductImageSettingVC.h"
#import "BaseModel.h"
#import "ZToastManager.h"
@interface ProductLinkVC ()<UITextViewDelegate>
{
    
}
@property(nonatomic,strong)UIPlaceholderTextView *mTextView;
@property(nonatomic,strong)UILabel *mInfoLabel;
@property(nonatomic,strong)UIButton *mBtn0;
@property(nonatomic,strong)UIButton *mBtn1;
@property(nonatomic,strong)UIButton *mBtn2;
@end

@implementation ProductLinkVC
@synthesize type=_type;
@synthesize rank_id=_rank_id;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.rightTitle =@"下一步";
    self.navTitle =@"发布单品";
    [UMengShare event:CK_LINKCREATE acc:[_rank_id integerValue]];
    [self initUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.enableBackGesture=YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
      DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)dealloc
{
    self.mTextView=nil;
    self.mInfoLabel=nil;
    self.mBtn0=nil;
    self.mBtn1=nil;
    self.mBtn2=nil;
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            self.navigationController.enableBackGesture=YES;
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
            case 1:
        {
            [self newAction];
        }
            break;
        default:
            break;
    }
}

-(void)newAction
{
//    self.mTextView.text=@"http://detail.tmall.com/item.htm?id=43765502699";
    if (self.mTextView.text.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请复制商品链接地址!"];
        return;
    }
    [self fetchItemData:self.mTextView.text urlstr:@""];
    
}
//
-(UIPlaceholderTextView*)mTextView
{
    if (!_mTextView) {
        self.mTextView =[[UIPlaceholderTextView alloc]initWithFrame:CGRectZero];
        self.mTextView.backgroundColor =[UIColor clearColor];
        self.mTextView.delegate = self;
        self.mTextView.layer.borderColor =UIColorFromRGB(0xcccccc).CGColor;
        self.mTextView.layer.borderWidth =0.5;
        self.mTextView.layer.cornerRadius=3;
        self.mTextView.placeholder = @"复制商品链接地址\n支持淘宝、京东、亚马逊、eBay等电商网站及APP";
        self.mTextView.textColor = [UIColor darkGrayColor];
        self.mTextView.font =FONT(14);
        self.mTextView.returnKeyType = UIReturnKeyDone;
    }
    return _mTextView;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.mTextView resignFirstResponder];
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

//
-(UILabel*)mInfoLabel
{
    if (!_mInfoLabel) {
        self.mInfoLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        self.mInfoLabel.backgroundColor = [UIColor clearColor];
        self.mInfoLabel.textColor = UIColorFromRGB(0x808080);
        self.mInfoLabel.textAlignment = NSTextAlignmentLeft;
        self.mInfoLabel.font = FONT(16);
        self.mInfoLabel.text =@"您可以通过:";
        self.mInfoLabel.numberOfLines=0;
    }
    return _mInfoLabel;
}
//
-(UIButton*)mBtn0
{
    if (!_mBtn0) {
        self.mBtn0 =[UIButton buttonWithType:UIButtonTypeCustom];
        self.mBtn0.backgroundColor =[UIColor clearColor];
        self.mBtn0.enabled=NO;
        [self.mBtn0 setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        self.mBtn0.titleLabel.font = FONT(14);
        self.mBtn0.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
        UIImage *image =[UIImage imageNamed:@"share_ico.png"];
        [self.mBtn0 setImage:image forState:UIControlStateNormal];
        [self.mBtn0 setTitle:@"  APP分享复制链接" forState:UIControlStateNormal];

    }
    return _mBtn0;
}
-(UIButton*)mBtn1
{
    if (!_mBtn1) {
        self.mBtn1 =[UIButton buttonWithType:UIButtonTypeCustom];
        self.mBtn1.backgroundColor =[UIColor clearColor];
        self.mBtn1.enabled=NO;
        [self.mBtn1 setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        self.mBtn1.titleLabel.font = FONT(14);
        self.mBtn1.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
        [self.mBtn1 setImage:[UIImage imageNamed:@"safari.png"] forState:UIControlStateNormal];
        [self.mBtn1 setTitle:@"  浏览器地址栏复制链接" forState:UIControlStateNormal];
        
    }
    return _mBtn1;
}
-(UIButton*)mBtn2
{
    if (!_mBtn2) {
        self.mBtn2 =[UIButton buttonWithType:UIButtonTypeCustom];
        self.mBtn2.backgroundColor =[UIColor clearColor];
        self.mBtn2.enabled=NO;
        [self.mBtn2 setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        self.mBtn2.titleLabel.font = FONT(14);
        self.mBtn2.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;;
        [self.mBtn2 setImage:[UIImage imageNamed:@"vote_ico.png"] forState:UIControlStateNormal];
        [self.mBtn2 setTitle:@"  好友推荐复制链接" forState:UIControlStateNormal];
        
    }
    return _mBtn2;
}
//
-(void)initUI
{
    self.mTextView.frame= CGRectMake(PHOTO_FRAME_WIDTH*2, self.navHeight+PHOTO_FRAME_WIDTH*2, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*10);
    [self.view addSubview:self.mTextView];
    self.mInfoLabel.frame= CGRectMake(PHOTO_FRAME_WIDTH*2, self.mTextView.frame.origin.y+self.mTextView.frame.size.height+PHOTO_FRAME_WIDTH*2, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*2);
    [self.view addSubview:self.mInfoLabel];
    self.mBtn0.frame =CGRectMake(PHOTO_FRAME_WIDTH*4, self.mInfoLabel.frame.origin.y+self.mInfoLabel.frame.size.height+PHOTO_FRAME_WIDTH*2, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*2);
    [self.view addSubview:self.mBtn0];
    //
    self.mBtn1.frame =CGRectMake(PHOTO_FRAME_WIDTH*4, self.mBtn0.frame.origin.y+self.mBtn0.frame.size.height+PHOTO_FRAME_WIDTH*2, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*2);
    [self.view addSubview:self.mBtn1];
    //
    self.mBtn2.frame =CGRectMake(PHOTO_FRAME_WIDTH*4, self.mBtn1.frame.origin.y+self.mBtn1.frame.size.height+PHOTO_FRAME_WIDTH*2, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*2);
    [self.view addSubview:self.mBtn2];
}
-(void)fetchItemData:(NSString*)data urlstr:(NSString*)urlstr
{
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:data forKey:@"data"];
    [dict setValue:urlstr forKey:@"url"];
    __weak typeof (self) wSelf = self;
     [[ZToastManager ShardInstance] showprogress];
    [BaseModel fetchItem:dict success:^(AFHTTPRequestOperation *operation, id result) {
        [wSelf parserMoreObject:result];
    } failure:^(NSError *error) {
         [[ZToastManager ShardInstance] hideprogress];
    }];
}
-(void)parserMoreObject:(id)resultObject
{
    if (resultObject==nil) {
        [[ZToastManager ShardInstance] hideprogress];
        return;
    }
    int code =[[resultObject objectForKey:@"code"] intValue];
    if (code<1) {
        [[ZToastManager ShardInstance] hideprogress];
        [[ZToastManager ShardInstance] showtoast:[resultObject objectForKey:@"msg"]];
        
        return;
    }else if (code==1)
    {
        [[ZToastManager ShardInstance] hideprogress];
        id data =[resultObject objectForKey:@"data"];
        if (data&&[data isKindOfClass:[NSDictionary class]]) {
            ProductImageSettingVC *pimage =[[ProductImageSettingVC alloc] init];
            pimage.dictionary =data;
            pimage.type=self.type;
            pimage.rank_id=self.rank_id;
            [self.navigationController pushViewController:pimage animated:YES];
            pimage=nil;
        }
        
    }else
    {
        NSString *urlstr =[NSString stringWithFormat:@"%@",[resultObject objectForKey:@"url"]];
        [self scrapywithUrl:urlstr];
    }

}

-(void)scrapywithUrl:(NSString*)str
{
    if(str.length==0||[str isEqualToString:@"(null)"])
    {
        [[ZToastManager ShardInstance] hideprogress];
        return;
    }
    NSString *data = [NSString stringWithContentsOfURL:[NSURL URLWithString:str] encoding:NSASCIIStringEncoding error:nil];
    [self fetchItemData:data urlstr:str];
}
@end
